<template>
  <!--区域经理-->
  <div class="jingli">
    <div class="header">
      <div class="employee" @click="toggle">
        <span>{{employee.employeeName}}</span><img src="../../assets/xiala.png" alt="" width="12">
      </div>
      <ul v-show="isShow" class="employeelist">
        <li v-for="(item,index) in employeeList" @click="check(item)">{{item.employeeName}}</li>
      </ul>
      <wv-cell
        title=""
        :value="startTime"
        is-link
        @click="openPicker('startDatePicker')"
      />
      <wv-datetime-picker
        type="date"
        ref="startDatePicker"
        :startDate="startDate"
        :endDate="endDate"
        v-model="beginDate"
        @confirm="onConfirmStartDate"
      />
      <div class="line"></div>
      <wv-cell
        title=""
        :value="endTime"
        is-link
        @click="openPicker('endDatePicker')"
      />
      <wv-datetime-picker
        type="date"
        ref="endDatePicker"
        :endDate="endDate"
        v-model="finshDate"
        @confirm="onConfirmEndTime"
      />
    </div>
    <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }">
      <wv-group
        v-infinite-scroll="getList"
        infinite-scroll-disabled="loading"
        infinite-scroll-distance="10"
      >
        <list :achievementList="achievementList" v-on:changeActiveIndex="getChangeActiveIndex"></list>
      </wv-group>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import list from '../template/yeji'
  import {getMyEmployee, employeeAchievement} from '../../../test/unit/http'
  import {Toast} from 'we-vue'

  export default {
    name: '',
    props: {},
    data() {
      return {
        isShow: false,
        employee: {employeeName: '销售'},
        startDate: new Date(new Date().setDate(new Date().getDate() - 31)),
        endDate: new Date(),
        beginDate: new Date(),
        finshDate: new Date(),
        employeeList: [],
        achievementList: [],
        isVipCard: '',
        wrapperHeight: 0,
        page: 1,
        rows: 5,
        loading: false
      };
    },
    methods: {
      toggle() {
        this.isShow = !this.isShow
      },
      check(item) {
        this.employee = item;
        this.isShow = !this.isShow;
        this.achievementList = [];
        this.getList();
      },
      openPicker(ref) {
        this.$refs[ref].open()
      },
      getList() {
        let params = {
          startDate: this.startTime,
          endDate: this.endTime,
          queryEmployeeId: this.employee.employeeId,
          isVipCard: this.isVipCard,
          page: this.page,
          rows: this.rows
        };
        this.getData(params)
      },
      getData(params) {
        this.loading = true;
        employeeAchievement(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            let data = res.object;
            let list = data.rows;
            let pages = data.pages;
            this.pages = pages;
            let totalNum = data.totalNum;
            this.totalNum = totalNum;
            if (list.length > 0) {
              this.achievementList = this.achievementList.concat(list);
            }
            if (this.page < this.pages) {
              this.page++;
              this.loading = false
            } else {
              this.loading = true
            }
          } else {
            Toast.fail(res.info)
          }
        })
      },
      getMyEmployeeData() {
        getMyEmployee({}).then(res => {
          console.log(res);
          if (res.code == 100) {
            this.employeeList = res.object;
          } else {
            Toast.fail(res.info)
          }
        })
      },
      getChangeActiveIndex(activeIndex) {
        this.page = 1;
        if (activeIndex == 0) {
          this.isVipCard == '';
        }
        if (activeIndex == 1) {
          this.isVipCard = 1;
        }
        if (activeIndex == 2) {
          this.isVipCard = 2;
        }
        this.achievementList = [];
        this.getList();
      },
      onConfirmStartDate(value) {
        let message = '';
        if (value !== null && typeof value === 'object') {
          let year = value.getFullYear();
          let month = value.getMonth() + 1;
          if (month < 10) {
            month = '0' + month;
          }
          let date = value.getDate();
          if (date < 10) {
            date = '0' + date;
          }
          message = `${year}-${month}-${date}`
        } else {
          message = value
        }
        this.page = 1;
        this.achievementList = [];
        this.getList();
      },
      onConfirmEndTime(value) {
        let message = '';
        if (value !== null && typeof value === 'object') {
          let year = value.getFullYear();
          let month = value.getMonth() + 1;
          if (month < 10) {
            month = '0' + month;
          }
          let date = value.getDate();
          if (date < 10) {
            date = '0' + date;
          }
          message = `${year}-${month}-${date}`
        } else {
          message = value
        }
        this.page = 1;
        this.achievementList = [];
        this.getList();
      }
    },
    created() {
      this.getMyEmployeeData();
    },
    computed: {
      startTime: {
        get: function () {
          let value = this.beginDate;
          let message = '';
          if (value !== null && typeof value === 'object') {
            let year = value.getFullYear();
            let month = value.getMonth() + 1;
            if (month < 10) {
              month = '0' + month;
            }
            let date = value.getDate();
            if (date < 10) {
              date = '0' + date;
            }
            message = `${year}-${month}-${date}`;
          } else {
            message = value;
          }
          return message;
        },
        set: function (date) {
          return date;
        }
      },
      endTime: {
        get: function () {
          let value = this.finshDate;
          let message = '';
          if (value !== null && typeof value === 'object') {
            let year = value.getFullYear();
            let month = value.getMonth() + 1;
            if (month < 10) {
              month = '0' + month;
            }
            let date = value.getDate();
            if (date < 10) {
              date = '0' + date;
            }
            message = `${year}-${month}-${date}`;
          } else {
            message = value;
          }
          return message;
        },
        set: function (date) {
          return date;
        }
      }
    },
    components: {
      list
    }
  }
</script>
<style scoped lang="less" rel="stylesheet/less">
  .header {
    display: flex;
    padding: 10px;
    position: relative;
    margin-bottom: 10px;
    overflow: visible;
    z-index: 200;
    border-bottom: 1px solid #ddd;

    .employeelist {
      position: absolute;
      width: 88%;
      top: 60px;
      left: 10px;
      right: 10px;
      text-align: left;
      padding: 10px;
      border: 1px solid #eee;
      background-color: #fff;
    }

    .employee {
      flex: 0 115px;
      color: #999;
      padding-top: 10px;

      img {
        vertical-align: middle;
        margin-left: 10px;
      }
    }

    .line {
      width: 24px;
      height: 1px;
      background-color: #ddd;
      content: '';
      display: block;
      margin: 19px 5px 0;
    }
  }

  .weui-cell:before {
    border-top: none;
  }

  .weui-cell_access {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    color: inherit;
    border: 1px solid #ddd;
    padding: 7px 4px;
    border-radius: 5px;
  }

  .weui-cell_access:last-child {
    margin-right: 0;
  }

</style>

